import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class InsertBook extends JDialog implements ActionListener {
    JLabel insertBook;
    Object name[] = {"编号", "书名", "出版社", "出版日期"};
    Object a[][] = new Object[1][5];
    JTable table;
    JButton insert;
    Connection con;
    Statement sql;
    ResultSet rs;

    InsertBook() {
        insertBook = new JLabel("请输入新书信息:");
        table = new JTable(a, name);
        insert = new JButton("确定插入");
        setLayout(new FlowLayout());
        add(insertBook);
        add(table);
        add(insert);
        setBounds(100, 100, 800, 200);
        setVisible(true);
        insert.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ee) {
            System.out.println("" + ee);
        }
        try {
            con = DriverManager.getConnection(Main.DB_URL, Main.DB_USER_NAME, Main.DB_PASSWORD);
            sql = con.createStatement();
            System.out.println(a[0][0] + " " + a[0][1] + " " + a[0][2] + " " + a[0][3]);
            int k = sql.executeUpdate("INSERT INTO insertBook VALUES('" + a[0][0] + "','" + a[0][1] + "','" + a[0][2] + "','" + a[0][3] + "')");
            if (k == 1)
                JOptionPane.showMessageDialog(this, "插入成功", "成功", JOptionPane.PLAIN_MESSAGE);
            setVisible(false);
            con.close();
        } catch (SQLException ee) {
            JOptionPane.showMessageDialog(this, "插入失败", "失败", JOptionPane.ERROR_MESSAGE);
            System.out.println(e);
        }
    }
}
